<?php 
namespace app\common\Api;

// 组合一维数组
class Category {
	static public function  UnlimitedLevel($cate,$html='--',$pid=0,$level=0){
		$arr = array();
		foreach ($cate as $v) {
			if($v['pid']==$pid){
				$v['level'] = $level+1;
				$v['html'] = str_repeat($html, $level);
				$arr[] = $v;
				$arr = array_merge($arr,self::UnlimitedLevel($cate,$html,$v['id'],$level+1));

			}
		}
		return $arr;
	}
	// 组合多维数组
	static public function  UnlimitedLayer( $cate, $name='Child', $pid=0 ){
		$arr = array();
		foreach ($cate as $v) {
			if ( $v['pid'] == $pid ) {
				$v[$name] = self::UnlimitedLayer( $cate, $name ,$v['id']);
				$arr[] = $v;
			}
		}
		return $arr;
	}
	// 传递一样个子id 返回所有的祖先
	static public function getParents( $cate , $id ){
		$arr = array();
		foreach ($cate as $v ) {
			if ($v['id'] == $id) {
				$arr[] = $v;
				$arr = array_merge(self::getParents($cate,$v['pid']),$arr);//交换传参数位置 调换顺序
			}
		}
		return $arr;

	}

	// 传递一个父ID，返回所有子分类ID
	static public function getChildsId ($cate, $pid){
		$arr = array();
		foreach ( $cate as $v ) {
			if ( $v['pid'] == $pid ) {
				$arr[] = $v['id'];
				$arr = array_merge( $arr, self::getChildsId( $cate, $v['id']));
			}
		}
		return $arr;
	}

	// 传递一个父ID 返回所有子分类
	static public function getChilds( $cate,$pid ){
		$arr = array();
		foreach ( $cate as $v ) {
			if ($v['pid'] == $pid) {
				$arr[] = $v;
				$arr = array_merge(self::getChilds($cate,$v['id']),$arr);
				
			}
		}
		return $arr;
	}

}
?>